package org.shoukaiseki.common.validation;

import org.shoukaiseki.common.utils.NumberUtils;
import org.springframework.stereotype.Service;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 数字不能小于0,为null忽略校验,还需要 NotNull 注解
 */
@Service
public class NumberOnlyGreaterZeroImpl implements ConstraintValidator<NumberOnlyGreaterZero, Number> {

    @Override
    public void initialize(NumberOnlyGreaterZero constraintAnnotation) {

    }

    @Override
    public boolean isValid(Number value, ConstraintValidatorContext constraintValidatorContext) {
        if(value==null){
            return true;
        }
        if (NumberUtils.isGreaterZero(value)) {
           return true;
        }
        return false;
    }
}
